package com.qfedu.service.impl;

import com.qfedu.dao.Debit_cardDao;
import com.qfedu.dao.MoneyRateDao;
import com.qfedu.dao.impl.Debit_cardDaoImpl;
import com.qfedu.dao.impl.MoneyRateDaoImpl;
import com.qfedu.pojo.Debit_card;
import com.qfedu.pojo.MoneyRate;
import com.qfedu.service.SaveService;
import com.qfedu.utils.JdbcUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

public class SaveServiceImpl implements SaveService {

    Debit_cardDao debit_cardDao = new Debit_cardDaoImpl();

    MoneyRateDao moneyRateDao = new MoneyRateDaoImpl();

    @Override
    public Debit_card queryByCardnumber(String cardnumber) {
        return debit_cardDao.queryDebit_cardByCardnumber(cardnumber);
    }

    @Override
    public int currentToFixed(Debit_card debit_card, Date date, BigDecimal money, BigDecimal fixed_money) {
        Connection connection = JdbcUtils.getConnection();
        int i;
        try {
            JdbcUtils.begin(connection);

            debit_cardDao.currentToFixed(debit_card,date,money,fixed_money,connection);
            JdbcUtils.commit(connection);
            i=1;
        } catch (Exception e) {
            JdbcUtils.rollback(connection);
            i=0;

        }finally {
            try {
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    @Override
    public int fixedToCurrent(Debit_card debit_card, Date date, BigDecimal money, BigDecimal fixed_money) {
        Connection connection = JdbcUtils.getConnection();
        int i;
        try {
            JdbcUtils.begin(connection);

            debit_cardDao.fixedToCurrent(debit_card,date,money,fixed_money,connection);
            JdbcUtils.commit(connection);
            i=1;
        } catch (Exception e) {
            JdbcUtils.rollback(connection);
            i=0;

        }finally {
            try {
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    @Override
    public int updateTime(String cardnumber, String time) {
        return debit_cardDao.updateTime(cardnumber,time);
    }

    @Override
    public MoneyRate findRate(String bank_name, String time) {
        return moneyRateDao.findRate(bank_name,time);
    }
}
